import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

import 'income.dart';
import 'invitation.dart';
import 'model/tsinvit_entity.dart';

///我是推手
class Ts extends StatefulWidget {
  int index;

  Ts(this.index);

  @override
  _TsState createState() => _TsState();
}

class _TsState extends State<Ts> with SingleTickerProviderStateMixin {
  MyPageState ps = MyPageState.LOADING;
  TabController _tabController;
  ScrollController _scrollController;
  List<Widget> widgets = [];
  TsinvitEntity tsinvitEntity;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController = ScrollController();
    _tabController = TabController(length: 3, vsync: this);
    widgets.add(Invitation(1));
    widgets.add(Invitation(2));
    widgets.add(InCome(3));
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return getBody();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _scrollController.dispose();
    _tabController.dispose();

    super.dispose();
  }

  getAppBar() {
    _tabController.animateTo(widget.index);
    return AppBar(
      brightness: Brightness.light,
      backgroundColor: Colours.white,
      title: Text(
        '我是推手',
        style: TextStyle(
            fontSize: ScreenUtil().setSp(34),
            fontFamily: Font.PingFangBlod,
            color: Colours.appC1),
      ),
      centerTitle: true,
      elevation: 0.0,
      leading: InkWell(
          highlightColor: Color(0xFFB3E5FC),
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            padding: EdgeInsets.fromLTRB(12.0, 0.0, 12.0, 0.0),
            child: Image.asset(
              Utils.getImgPath('return_left'),
              width: 18.0,
              height: 20.0,
            ),
          )),
      bottom: TabBar(
          controller: _tabController,
          indicatorColor: Color(0xFFFFFFFF),
          indicatorWeight: ScreenUtil().setWidth(1),
          indicatorPadding: EdgeInsets.fromLTRB(8, 0, 8, 0),
          labelPadding: EdgeInsets.only(bottom: 0),
          unselectedLabelColor: Colours.appC1,
          labelColor: Color(0xFFE3393C),
          tabs: <Widget>[
            myTab(tsinvitEntity.data.direct, "直接邀请"),
            myTab(tsinvitEntity.data.indirect, "间接邀请"),
            myTab(
                tsinvitEntity.data.money.isEmpty ? "0" : tsinvitEntity.data.money,
                "收益详情"),
          ]),
    );
  }

  myTab(String s, String t) {
    return Column(
      children: <Widget>[
        Container(
          child: Text(s),
        ),
        Container(
          margin: EdgeInsets.only(top: ScreenUtil().setWidth(20)),
          child: Text(t),
        ),
      ],
    );
  }

  void getData() {
    DioManager.getInstance().get(Api.USER_TSINVIT, null, (json) {
      tsinvitEntity = TsinvitEntity.fromJson(json);
      if (null == tsinvitEntity) {
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        NavigatorUtils.goBack(context);
        return;
      }
      if (tsinvitEntity.code != 200) {
        ToastUtil.showShortErrToast(tsinvitEntity.message);
        NavigatorUtils.goBack(context);
        return;
      }
      ps = MyPageState.HAVE_DATA;
      setState(() {});
    }, (err) {});
  }

  Widget getBody() {
    if (ps == MyPageState.LOADING) {
      return Scaffold(
        appBar: UiUtils.appBar(context, "我是推手"),
        body: LoadingPage(),
      );
    }
    if (ps == MyPageState.HAVE_DATA) {
      return Scaffold(
        appBar: getAppBar(),
        body: TabBarView(controller: _tabController, children: widgets),
      );
    }
  }
}
